﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Mvc.Ajax;

namespace System.Web.Mvc
{
    /// <summary>
    /// 分页控件生成类接口
    /// </summary>
    public interface IPaggerFormat
    {
        /// <summary>
        /// 获取或设置分页数据请求方式
        /// </summary>
        HttpMethod HttpMethod { get; set; }

        /// <summary>
        /// 获取分页控件HTML代码
        /// </summary>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页的索引，从0开始</param>
        /// <param name="pageSize">每页显示个数</param>
        /// <returns></returns>
        string GetFormatString(string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize);
    }

    /// <summary>
    /// 分页控件生成类基类
    /// </summary>
    public abstract class PaggerFormat : IPaggerFormat
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public PaggerFormat()
        {
            this.HttpMethod = HttpMethod.GET;
        }

        /// <summary>
        /// 获取或设置分页数据请求方式
        /// </summary>
        public HttpMethod HttpMethod { get; set; }

        /// <summary>
        /// 生成HttpGet方式的分页控件
        /// </summary>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页显示个数</param>
        /// <returns></returns>
        public abstract string GenerateGetFormatString(string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize);

        /// <summary>
        /// 生成HttpPost方式的分页控件
        /// </summary>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页显示个数</param>
        /// <returns></returns>
        public abstract string GeneratePostFormatString(string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize);

        /// <summary>
        /// 获取分页控件HTML代码
        /// </summary>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页的索引，从0开始</param>
        /// <param name="pageSize">每页显示个数</param>
        /// <returns></returns>
        public string GetFormatString(string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize)
        {

            switch (this.HttpMethod)
            {
                case HttpMethod.GET:
                    return GenerateGetFormatString(actionName, controllerName, routeValues, rowCount, pageIndex, pageSize);
                case HttpMethod.POST:
                    return GeneratePostFormatString(actionName, controllerName, routeValues, rowCount, pageIndex, pageSize);
                default:
                    return String.Empty;
            }
        }
    }

    /// <summary>
    /// 生成HTML分页控件的基类
    /// </summary>
    public abstract class HtmlPaggerFormat : PaggerFormat
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public HtmlPaggerFormat()
            : base()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="httpMethod">Http请求方式</param>
        public HtmlPaggerFormat(HttpMethod httpMethod)
        {
            this.HttpMethod = httpMethod;
        }

        /// <summary>
        /// 获取或设置HtmlHelper对象
        /// </summary>
        public HtmlHelper Helper { get; set; }
    }

    /// <summary>
    /// 生成Ajax分页控件的基类
    /// </summary>
    public abstract class AjaxPaggerFormat : PaggerFormat
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>
        public AjaxPaggerFormat(AjaxOptions options)
        {
            this.Options = options == null ? new AjaxOptions() : options;
            this.HttpMethod = String.IsNullOrEmpty(options.HttpMethod)
                || options.HttpMethod.Equals("get", StringComparison.CurrentCultureIgnoreCase)
                ? HttpMethod.GET : HttpMethod.POST;
        }

        /// <summary>
        /// 获取AjaxOptions对象
        /// </summary>
        public AjaxOptions Options { get; private set; }

        /// <summary>
        /// 获取或设置AjaxHelper对象
        /// </summary>
        public AjaxHelper Helper { get; set; }
    }

    /// <summary>
    /// HTML分页方式枚举值
    /// </summary>
    public enum HtmlPaggerType
    {
        /// <summary>
        /// 上一页|下一页
        /// </summary>
        PrevNext = 0,
        /// <summary>
        /// 数字
        /// </summary>
        Numeric = 1
    }

    /// <summary>
    /// Ajax分页方式枚举值
    /// </summary>
    public enum AjaxPaggerType
    {
        /// <summary>
        /// 上一页|下一页
        /// </summary>
        PrevNext = 0,
        /// <summary>
        /// 数字
        /// </summary>
        Numeric = 1,
        /// <summary>
        /// 更多
        /// </summary>
        More = 2
    }
}
